package com.liugan.magic.screen.layer;

import java.util.ArrayList;

import org.loon.framework.android.game.core.graphics.component.LButton;
import org.loon.framework.android.game.core.graphics.component.Layer;
import org.loon.framework.android.game.core.graphics.opengl.GLEx;
import org.loon.framework.android.game.core.graphics.opengl.LTexture;

import android.util.Log;

import com.liugan.magic.Constants;
import com.liugan.magic.actor.Illusionist;
import com.liugan.magic.actor.Key;
import com.liugan.magic.actor.base.BaseActor;
import com.liugan.magic.actor.still.Door;
import com.liugan.magic.map.Magicmap;
import com.liugan.magic.screen.PointXY;
import com.liugan.magic.sound.ASound;
import com.liugan.magic.utils.AnimationUtils;
import com.liugan.magic.utils.ImagesUtils4layer;
import com.liugan.magic.utils.ImagesUtils4start;

//layer 
public class GameLayer extends Layer{
	
	public boolean issucceed=false;
	Magicmap magicmap;
	String load="loading...";
	//SOUND
	LButton sound;
	//默认打开音乐
	private boolean issound=true;
	public boolean isIssound() {
		return issound;
	}
	
	public PointXY layertotalXY,layeroffsetXY,layerbeginXY;
	public ArrayList<BaseActor>getActorList(){
		return ActorList;
	}
	public Key getKey(){
		return key;
	}
	public Door getDoor(){
		return door;
	}
	
	private ArrayList<BaseActor> ActorList =new ArrayList<BaseActor>();
	private Key key;
	private Door door;
	//根据前景map来加载前景图片
	@Override
	public void paint(GLEx g) {
		Log.i("aaa", (magicmap==null)?"magicmap空":"magicmap不空");
		if (magicmap!=null){
			magicmap.drawFore(g,layertotalXY.x-layeroffsetXY.x, layertotalXY.y-layeroffsetXY.y);
			//显示钥匙
			if (key!=null&&!key.isVisible()){
				g.drawTexture(AnimationUtils.getinstance().key.getSpriteImage(1),Constants.ScreenWIDTH-80, 0);
			}
			setSound();
		}else{
			//g.drawImage(new LImage(Constants.imagespath_s +"armorgames.png"), 0, 0);
			g.drawString(load, getScreenWidth()/2, getScreenHeight()/2);
			Log.i("aaa", "loading");
		}
	}

	public void setSound(){
		//添加按钮
		if (sound==null){
			sound=new LButton(ImagesUtils4layer.getinstance().sound_b[0],null,Constants.ScreenWIDTH-50,Constants.ScreenHEIGHT-50){
				@Override
				public void doClick() {
						if (issound){
							issound=false;
							LTexture[] buttons = new LTexture[1];
							buttons[0]=ImagesUtils4layer.getinstance().sound_b[1];
							this.setImages(buttons);
							
							ASound.getInstance().quiet();
						}else{
							issound=true;
							LTexture[] buttons = new LTexture[1];
							buttons[0]=ImagesUtils4layer.getinstance().sound_b[0];
							this.setImages(buttons);
							ASound.getInstance().voice();
					}
				}
			};
			
			this.add(sound);
			//背景音乐
			ASound.getInstance().s617();
		}
	}
	public void addObject(BaseActor object, int x, int y) {
		super.addObject(object, x, y);
		if (!(object instanceof Illusionist)){
			object.offsetXY.setxy(x, y);
			object.totalXY.setxy(x, y);
			if (object instanceof Key){
				key=(Key) object;
			}else if (object instanceof Door){
				door=(Door) object;
			}else{
				ActorList.add(object);
			}
		}else{
			
		}
	}
	
	public GameLayer(PointXY layertotalXY,PointXY layeroffsetXY,PointXY layerbeginXY,Illusionist magic){
		
		super(Constants.ScreenWIDTH,Constants.ScreenHEIGHT, false);
		setActorDrag(false);
		setLocked(true);
		setLimitMove(true);
		setDelay(300);

		this.layertotalXY=layertotalXY;
		this.layeroffsetXY=layeroffsetXY;
		this.layerbeginXY=layerbeginXY;
		layeroffsetXY.setxy(200, 100);
		layertotalXY.setxy(200, 100);
		
		//在一开始   offsetXY 是等于 totalXY的
		this.addObject(magic,layeroffsetXY.x,layeroffsetXY.y);
		
	}
	public void setmap(Magicmap magicmap){
		this.magicmap=magicmap;
	}
	public void action(long elapsedTime) {
		if (issucceed){
			
			
			
		}
		//判断是否结束  进而处理
		//ended
		//这里的结束就是  主角掉落,从新生成主角.并将主角生成到一个暂时位置.
	}
}
